// Package preference_api
// desc: x
// author: Locker
// date: 2024/6/10
// version: 1.0.0
package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/widget"
	"log"
	"time"
)

func main() {
	a := app.NewWithID("com.xaicode.learn.fyne.tutorial.preferences")
	w := a.NewWindow("Timeout")

	var timeout time.Duration

	timeoutSelector := widget.NewSelect([]string{"10s", "30s", "1m"}, func(selected string) {
		switch selected {
		case "10s":
			timeout = 10 * time.Second
		case "30s":
			timeout = 30 * time.Second
		case "1m":
			timeout = time.Minute
		}

		a.Preferences().SetString("AppTimeout", selected)
	})

	timeoutSelector.SetSelected(a.Preferences().StringWithFallback("AppTimeout", "10s"))

	//go func() {
	//	time.Sleep(timeout)
	//	a.Quit()
	//}()
	log.Printf("timeout: %v\n", timeout)

	w.SetContent(timeoutSelector)

	// system tray menu
	if desk, ok := a.(desktop.App); ok {
		m := fyne.NewMenu("MyApp",
			fyne.NewMenuItem("Show", func() {
				log.Println("Tapped Show")
			}),
		)
		desk.SetSystemTrayMenu(m)
		path, _ := fyne.LoadResourceFromPath("C:\\Users\\Lenovo\\go\\pkg\\mod\\fyne.io\\fyne\\v2@v2.4.5\\theme\\icons\\fyne.png")
		desk.SetSystemTrayIcon(path)
	}

	w.ShowAndRun()
}
